﻿using L5_Event.Event;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;

namespace L5_Event.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private readonly IEventAggregator eventAggregator;
        private readonly IDialogService dialogService;

        public ObservableCollection<string> LogMessages { get; set; } = new ObservableCollection<string>();

        #region 订阅
        public DelegateCommand SubscribeLogCommand { get; set; }
        void ExcuteSubscribeLog()
        {
            eventAggregator.GetEvent<LogEvent>().Subscribe(msg =>
            {
                LogMessages.Add(msg);
            }, msg =>
            {
                return msg.StartsWith("[Window]");
            });
        }
        #endregion

        #region 记录
        public DelegateCommand<bool?> RecordCommand { get; set; }
        void ExcuteRecord(bool? isShow)
        {
            if (isShow == true)
            {
                eventAggregator.GetEvent<LogEvent>().Publish($"[Window] [{DateTime.Now}] MainWindow Record");
            }
            else
            {
                eventAggregator.GetEvent<LogEvent>().Publish($"[Log] [{DateTime.Now}] MainWindow Record");
            }
        }
        #endregion

        #region 弹窗
        public DelegateCommand ShowDialogCommand { get; set; }
        void ExcuteShowDialog()
        {
            dialogService.Show("DialogView");
        }
        #endregion


        #region 线程控制-订阅
        public DelegateCommand SubscribeLogCommand2 { get; set; }
        void ExcuteSubscribeLog2()
        {
            eventAggregator.GetEvent<LogEvent>().Subscribe(msg =>
            {
                LogMessages.Add(msg);
            }, ThreadOption.UIThread);
        }
        #endregion

        #region 线程控制-记录
        public DelegateCommand RecordCommand2 { get; set; }
        async void ExcuteRecord2()
        {
            await Task.Run(async () =>
            {
                eventAggregator.GetEvent<LogEvent>().Publish($"[{Thread.CurrentThread.ManagedThreadId}] [{DateTime.Now}] MainWindow Record");
                await Task.Delay(5000);
            });
        }
        #endregion

        #region 取消订阅1
        public DelegateCommand SubscribeLogCommand3 { get; set; }
        void ExcuteSubscribeLog3()
        {
            eventAggregator.GetEvent<LogEvent>().Subscribe(LogEventCallback3);
        }

        void LogEventCallback3(string msg)
        {
            LogMessages.Add(msg);
        }

        public DelegateCommand UnSubscribeLogCommand3 { get; set; }
        void ExcuteUnSubscribeLog3()
        {
            eventAggregator.GetEvent<LogEvent>().Unsubscribe(LogEventCallback3);
        }
        #endregion

        #region 取消订阅2
        SubscriptionToken _token;
        public DelegateCommand SubscribeLogCommand4 { get; set; }
        void ExcuteSubscribeLog4()
        {
            _token = eventAggregator.GetEvent<LogEvent>().Subscribe(msg =>
            {
                LogMessages.Add(msg);
            });
        }

        public DelegateCommand UnSubscribeLogCommand4 { get; set; }
        void ExcuteUnSubscribeLog4()
        {
            eventAggregator.GetEvent<LogEvent>().Unsubscribe(_token);
        }
        #endregion

        #region 强引用订阅
        public DelegateCommand SubscribeLogCommand5 { get; set; }
        void ExcuteSubscribeLog5()
        {
            eventAggregator.GetEvent<LogEvent>().Subscribe(msg =>
            {
                LogMessages.Add(msg);
            }, true);
        }
        #endregion

        public MainWindowViewModel(IEventAggregator eventAggregator, IDialogService dialogService)
        {
            this.eventAggregator = eventAggregator;
            this.dialogService = dialogService;

            SubscribeLogCommand = new DelegateCommand(ExcuteSubscribeLog);
            RecordCommand = new DelegateCommand<bool?>(ExcuteRecord);
            ShowDialogCommand = new DelegateCommand(ExcuteShowDialog);

            SubscribeLogCommand2 = new DelegateCommand(ExcuteSubscribeLog2);
            RecordCommand2 = new DelegateCommand(ExcuteRecord2);

            SubscribeLogCommand3 = new DelegateCommand(ExcuteSubscribeLog3);
            UnSubscribeLogCommand3 = new DelegateCommand(ExcuteUnSubscribeLog3);

            SubscribeLogCommand4 = new DelegateCommand(ExcuteSubscribeLog4);
            UnSubscribeLogCommand4 = new DelegateCommand(ExcuteUnSubscribeLog4);

            SubscribeLogCommand5 = new DelegateCommand(ExcuteSubscribeLog5);
        }
    }
}